package com.zhoufeng.udp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

public class Monitor implements Runnable {
	
	Logger logger = Logger.getLogger(Monitor.class);

	private List<Thread> monitoredThread; // 要监控的线程名

	public Monitor(List<Thread> monitoredThread) {
		this.monitoredThread = monitoredThread;
	}

	public void run() {
		while (true) {
			monitor();
			try {
				TimeUnit.MILLISECONDS.sleep(10000);
			} catch (InterruptedException e) {
				// TODO 记日志
				e.printStackTrace();
			}
		}

	}

	/**
	 * 监控主要逻辑
	 */
	private void monitor() {
		for (int i = 0; i < monitoredThread.size(); i++) {

			// DatagramSocket client = null;
			// int port = new Integer(monitoredThread.get(i).getName());
			// try {
			// client = new DatagramSocket();
			//
			// // monitoredThread.get(i).stop();
			//
			// String s =
			// "FF FF FE 13 01 11 26 00 35 00 A0 02 5C 15 E9 82 25 FF FF BA 2C AB";
			//
			// InetAddress addr = InetAddress.getByName("localhost");
			// byte[] sendBuf;
			// // sendBuf = s.getBytes();
			// sendBuf = CommandUtil.getCommand(s);
			//
			//
			// // int port = new Integer(5000);
			// DatagramPacket sendPacket = new DatagramPacket(sendBuf,
			// sendBuf.length, addr, port);
			// client.send(sendPacket);
			//
			// byte[] recvBuf = new byte[Common.COMMAND_LENGTH];
			// DatagramPacket recvPacket = new DatagramPacket(recvBuf,
			// recvBuf.length);
			// client.setSoTimeout(1000);
			// client.receive(recvPacket);
			//
			// String recvStr =
			// CommandUtil.bytesToHexString(recvPacket.getData()); // new
			// // String(recvPacket.getData(),
			// // 0,
			// // recvPacket.getLength());
			// System.out.println("收到:" + recvStr);
			//
			//
			// } catch (Exception e) {
			// if (e instanceof SocketTimeoutException) {
			// System.out.println("socket超时！");
			// // monitoredThread.get(i).interrupt();
			// // monitoredThread.get(i).start();
			//
			// try {
			// monitoredThread.get(i).setFlag(false);
			// monitoredThread.get(i).join();
			// } catch (Exception e1) {
			// e1.printStackTrace();
			// }
			//
			// UDPServer thread = new UDPServer(port);
			// thread.start();
			// monitoredThread.set(i, thread);
			// }
			// } finally {
			// if (!client.isClosed()){
			// client.close();
			// }
			// }

			Thread.State state = monitoredThread.get(i).getState();
			// 获得指定线程状态

			//System.out.println(monitoredThread.get(i).getName() + "监控线程状态是:" + monitoredThread.get(i).getState());
			logger.debug(monitoredThread.get(i).getName() + "监控线程状态是:" + monitoredThread.get(i).getState());

			/*
			 * 如果监控线程为终止状态,则重启监控线程
			 */
			if (Thread.State.TERMINATED.equals(state)) {
				//System.out.println(monitoredThread.get(i).getName() + "监控线程已经终止,现在开始重启监控线程!");
				logger.debug(monitoredThread.get(i).getName() + "监控线程已经终止,现在开始重启监控线程!");
				monitoredThread.get(i).start();
			}
		}
	}

	/**
	 * 测试方法
	 */
	public static void main(String[] args) {

		UDPServer tm = new UDPServer(5000);
		tm.setName("5000");
		tm.start();
		UDPServer tm1 = new UDPServer(5001);
		tm1.setName("5001");
		tm1.start();
		List<Thread> list = new ArrayList<Thread>();
		list.add(tm);
		list.add(tm1);
		Thread m = new Thread(new Monitor(list));
		m.start();

	}

}
